package com.zhufeng.base.config;

import okhttp3.OkHttpClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: RestTemplateConfig
 * @Description 创建RestTemplate bean
 * @author 月夜烛峰
 * @date 2022/8/30 09:14
 */
@Configuration
public class RestTemplateConfig {

    //建立连接的超时时间
    private Integer connectTimeout = 3000;
    //请求内容超时时间
    private Integer readTimeout = 3000;


    /**
     * 没有实例化RestTemplate时，初始化RestTemplate
     * @return
     */
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        //使用ok http访问
        RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
        return restTemplate;
        //默认
        //return new RestTemplate();
    }

    /**
     * 使用OkHttpClient作为底层客户端
     * @return
     */
    private ClientHttpRequestFactory getClientHttpRequestFactory(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .build();
        return new OkHttp3ClientHttpRequestFactory(okHttpClient);
    }

    /**
     * HttpClient
     * @return
     */
    /**
    @Bean
    public HttpClientBuilder httpClientBuilder(HttpClientConnectionManager connectionManager) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 设置Http连接管理器
        httpClientBuilder.setConnectionManager(connectionManager);
        return httpClientBuilder;
    }
    */
    /**
     * 请求连接池配置
     * @return
     */
    /**
    @Bean
    public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(HttpClientBuilder httpClientBuilder) {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        // httpClient创建器
        httpRequestFactory.setHttpClient(httpClientBuilder.build());

        // 连接池获取请求连接的超时时间，不宜过长，必须设置/毫秒（超时间未拿到可用连接，会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool）
        httpRequestFactory.setConnectionRequestTimeout(connectTimeout);

        // 连接超时时间/毫秒（连接上服务器(握手成功)的时间，超过会抛出connect timeout）
        httpRequestFactory.setConnectTimeout(connectTimeout);

        // 数据读取超时时间(socketTimeout)/毫秒（服务器返回数据(response)的时间，超过抛出read timeout）
        httpRequestFactory.setReadTimeout(readTimeout);

        return httpRequestFactory;
    }
     */

    /**
     * RestTemplate
     * @param clientHttpRequestFactory 请求工厂
     * @return
     */
    /**
    @Bean
    // 如果项目不是微服务体系，不需要配置这个注解，此注解是为了自动发现服务
    @LoadBalanced
    public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory clientHttpRequestFactory) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        return restTemplate;
    }
     */
}
